{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "79c2f4e5",
   "metadata": {},
   "source": [
    "# Approximate H2 molecule energy near ground state\n",
    "\n",
    "In this example notebook, we'll walk through calculating the energy of a molecule given an estimate for the ground state using Azure Quantum. We will do that using a simple algorithm where we prepare the quantum register in a state near the ground state of the molecule using the Jordan-Wigner encoding, and evaluate the energy of the molecule by applying the Hamiltonian operator to that state. To be able to run the algorithm on near-term hardware, we will estimate the energy per Hamiltonian term, and then calculate the total estimated energy by adding the values of these terms."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "55b70453",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preparing Q# environment...\n"
     ]
    },
    {
     "data": {
      "application/json": "{\"LastUpdated\":\"2021-05-26T07:40:48.393022-07:00\",\"IsCompleted\":false,\"Description\":\"Reloading workspace\",\"Subtask\":null}",
      "text/plain": [
       "Reloading workspace."
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# First, import qsharp and qsharp.azure to be able to compile and submit the quantum program.\n",
    "import qsharp\n",
    "import qsharp.azure\n",
    "qsharp.reload()\n",
    "from Microsoft.Quantum.Chemistry.Hamiltonian import GetHamiltonianTermH2, ExpandedCoefficients_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9efb9be7",
   "metadata": {},
   "source": [
    "### 1. Load H2 molecule data\n",
    "\n",
    "The pre-generated Broombridge file included in this sample contains the details on the molecule's Hamiltonian. A Hamiltonian is an operator that calculates the energy of a molecule by acting on a qubit register that represents the molecule's quantum state by Jordan-Wigner encoding. If we prepare the molecule in the ground state, then we know that applying the Hamiltonian will calculate the ground state energy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "601fb1cb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/json": "{\"LastUpdated\":\"2021-05-26T07:40:54.079836-07:00\",\"IsCompleted\":false,\"Description\":\"Adding package microsoft.quantum.chemistry.jupyter\",\"Subtask\":null}",
      "text/plain": [
       "Adding package microsoft.quantum.chemistry.jupyter."
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from qdk.chemistry.broombridge import load_and_encode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f11f81ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "encoded_data_h2 = load_and_encode(\"../data/broombridge/hydrogen_0.2.yaml\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ff4fc44",
   "metadata": {},
   "source": [
    "This contains a tuple of the number of qubits, the fermionic Hamiltonian term coefficients and the energy offset.\n",
    "\n",
    "The index of each list in the second tuple is will be referred to as the \"term type\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "38c84985",
   "metadata": {},
   "outputs": [],
   "source": [
    "num_qubits, fermion_terms, _, energy_offset = encoded_data_h2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13735540",
   "metadata": {},
   "source": [
    "### 2. Resource estimation\n",
    "Before running on hardware, let's first estimate the quantum resources needed for measuring two-electron terms (\"term type\" = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "50f802c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'CNOT': 0,\n",
       " 'QubitClifford': 2,\n",
       " 'R': 0,\n",
       " 'Measure': 5,\n",
       " 'T': 0,\n",
       " 'Depth': 0,\n",
       " 'Width': 4,\n",
       " 'QubitCount': 4,\n",
       " 'BorrowedWidth': 0}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GetHamiltonianTermH2.estimate_resources(\n",
    "    nOp=12\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0129eef",
   "metadata": {},
   "source": [
    "We have 11 qubits on the IonQ system so this is feasible to run!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d2f1826",
   "metadata": {},
   "source": [
    "### 3. Running on Azure Quantum\n",
    "\n",
    "Connect to Azure Quantum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2353946a",
   "metadata": {},
   "outputs": [],
   "source": [
    "rid = \"\" # Enter your workspace's resource ID here\n",
    "location = \"West US\" # Enter your workspace's location here, e.g. \"West US\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "510a2069",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Connected to Azure Quantum workspace TestGuen in location westus.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'id': 'ionq.qpu', 'current_availability': 'Available', 'average_queue_time': 11},\n",
       " {'id': 'ionq.simulator', 'current_availability': 'Available', 'average_queue_time': 0}]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qsharp.azure.connect(\n",
    "   resourceId=rid,\n",
    "   location=location\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4343abac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading package Microsoft.Quantum.Providers.IonQ and dependencies...\n",
      "Active target is now ionq.simulator\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'id': 'ionq.simulator', 'current_availability': 'Available', 'average_queue_time': 0}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qsharp.azure.target(\"ionq.simulator\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9657ea3f",
   "metadata": {},
   "source": [
    "Run a single term on the IonQ simulator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ec77e9e8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 17\n",
      "   Job ID: 1db9ee2e-f4ca-43e1-bf27-7d4f87681e67\n",
      "Waiting up to 30 seconds for Azure Quantum job to complete...\n",
      "[7:15:42 AM] Current job status: Succeeded\n"
     ]
    }
   ],
   "source": [
    "n_op = 17\n",
    "result = qsharp.azure.execute(GetHamiltonianTermH2, nOp=n_op, shots=1000, jobName=f\"Hamiltonian term {n_op}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3988101c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'0': 0.5, '1': 0.5}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ba1bfe5",
   "metadata": {},
   "source": [
    "### 4. Loop over all Hamiltonian terms"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3500a530",
   "metadata": {},
   "source": [
    "Each Hamiltonian term has a term type, measurement operator and coefficient. To simplify this sample, we will move the coefficients into a flat list. Each nonzero coefficient will render a separate job that we send to Azure Quantum. To calculate the energy for that term, we map result from the job onto a -1 to 1 axis and multiply the value by the corresponding coefficient."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "22a79e06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.17120128499999998,\n",
       " 0.17120128499999998,\n",
       " -0.222796536,\n",
       " -0.222796536,\n",
       " 0.1686232915,\n",
       " 0.12054614575,\n",
       " 0.16586802525,\n",
       " 0.16586802525,\n",
       " 0.12054614575,\n",
       " 0.1743495025,\n",
       " 0.0,\n",
       " 0.0,\n",
       " -0.0453218795,\n",
       " -0.0453218795,\n",
       " 0.0,\n",
       " 0.0,\n",
       " 0.0453218795,\n",
       " 0.0453218795]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coeffs = []\n",
    "for term_type, terms in enumerate(fermion_terms):\n",
    "    for (qubits, coeff) in terms:\n",
    "        coeffs += ExpandedCoefficients_(coeff=coeff, termType=term_type)\n",
    "coeffs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0062e6c8",
   "metadata": {},
   "source": [
    "There are in total 18 coefficients corresponding to each combination of Hamiltonian term and Jordan-Wigner measurement operator. Start a job for each term that has a nonzero coefficient:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3cc6c5ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 0\n",
      "   Job ID: ff06a236-700f-4293-be37-0597ff54f470\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 1\n",
      "   Job ID: cf3a5c46-b33e-4fb2-8145-c33836051a16\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 2\n",
      "   Job ID: 97b64b6b-0418-4651-9eaa-d259af443992\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 3\n",
      "   Job ID: 31a8f734-2e58-4879-b9f7-0f53d98003ee\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 4\n",
      "   Job ID: 38131993-d98a-4a0a-a254-d1a05e801d87\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 5\n",
      "   Job ID: d0cadafb-cdb9-4007-969a-c48a2eebbe66\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 6\n",
      "   Job ID: 7270a725-6272-4183-881a-ba4e26e19711\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 7\n",
      "   Job ID: d8363d0a-7412-4f8f-ae0a-92a9b1979105\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 8\n",
      "   Job ID: 8e4b3b3a-5822-413c-b8db-5ccc40234630\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 9\n",
      "   Job ID: 8f485eac-150f-44fd-8af4-f5cec7830d46\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 12\n",
      "   Job ID: 20bebd41-9bfe-49c8-9618-bab7bb4dc420\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 13\n",
      "   Job ID: 90eea668-9860-491c-9319-380aa69b8e20\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 16\n",
      "   Job ID: 688d3468-b41a-496f-8908-c8e854e10062\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.simulator...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 17\n",
      "   Job ID: 358c1898-efc3-45f2-96f0-3f2a7873e4bd\n"
     ]
    }
   ],
   "source": [
    "jobs = [\n",
    "    qsharp.azure.submit(GetHamiltonianTermH2, nOp=n_op, shots=1000, jobName=f\"Hamiltonian term {n_op}\") \n",
    "    for n_op, coeff in enumerate(coeffs) if coeff != 0.0\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "002adcf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'1': 1.0},\n",
       " {'1': 1.0},\n",
       " {'0': 1.0},\n",
       " {'0': 1.0},\n",
       " {'0': 1.0},\n",
       " {'1': 1.0},\n",
       " {'1': 1.0},\n",
       " {'1': 1.0},\n",
       " {'1': 1.0},\n",
       " {'0': 1.0},\n",
       " {'0': 0.5, '1': 0.5},\n",
       " {'0': 0.5, '1': 0.5},\n",
       " {'0': 0.5, '1': 0.5},\n",
       " {'0': 0.5, '1': 0.5}]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = [qsharp.azure.output(j.id) for j in jobs]\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f90d71ed",
   "metadata": {},
   "source": [
    "### 5. Estimate energy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3742b6b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calc_energy(coeffs, results):\n",
    "    return sum([(2. * res.get(\"0\", 0.0) - 1.) * coeff for coeff, res in zip(coeffs, results)]) + energy_offset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ee6339e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.1166856359999997"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calc_energy(coeffs, results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fc3eb7c",
   "metadata": {},
   "source": [
    "Now we are ready to run on hardware! Switch to the QPU."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "7c5ec02b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading package Microsoft.Quantum.Providers.IonQ and dependencies...\n",
      "Active target is now ionq.qpu\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'id': 'ionq.qpu', 'current_availability': 'Available', 'average_queue_time': 12252}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qsharp.azure.target(\"ionq.qpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "4e045032",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 0\n",
      "   Job ID: 7c8839d0-a553-44db-88a1-1c4591d19196\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 1\n",
      "   Job ID: d73bcfc3-aefc-4a9f-8fc5-e71b60a74d30\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 2\n",
      "   Job ID: bfd41d0e-96ca-4583-9aee-6425e76eddf8\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 3\n",
      "   Job ID: e6f663c6-f23f-4db1-9973-ab83f6fdeaf0\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 4\n",
      "   Job ID: 0beb6a43-7149-416b-8338-4a9d875443bd\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 5\n",
      "   Job ID: 218d092a-cff2-4c41-b033-0f0ea0b87024\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 6\n",
      "   Job ID: 687aa612-6898-40e8-b502-c7e0c547c588\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 7\n",
      "   Job ID: d0f1c1b4-62e8-48a4-bd85-5df0fa3485c8\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 8\n",
      "   Job ID: 54313164-412f-46b8-bf37-3a760226e2c7\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 9\n",
      "   Job ID: 0ea376fd-8e72-478c-bfbe-c4f5c022e040\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 12\n",
      "   Job ID: 0eecd3a6-ed34-4065-be6a-d9f848fcbbe6\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 13\n",
      "   Job ID: 0425662e-85ad-4ba5-b9d3-12c16ffa5ea1\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 16\n",
      "   Job ID: aa93563a-6def-4684-b217-353593886598\n",
      "Submitting Microsoft.Quantum.Chemistry.Hamiltonian.GetHamiltonianTermH2 to target ionq.qpu...\n",
      "Job successfully submitted for 1000 shots.\n",
      "   Job name: Hamiltonian term 17\n",
      "   Job ID: 71b894a1-e252-40ae-9274-ffb1063163ae\n"
     ]
    }
   ],
   "source": [
    "jobs_hw = [\n",
    "    qsharp.azure.submit(GetHamiltonianTermH2, nOp=n_op, shots=1000, jobName=f\"Hamiltonian term {n_op}\") \n",
    "    for n_op, coeff in enumerate(coeffs) if coeff != 0\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f65fb519",
   "metadata": {},
   "source": [
    "Check if the jobs succeeded."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "927be51a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Succeeded | Hamiltonian term 0 | ionq.qpu | 2021-05-06T01:20:30.9254783Z | 2021-05-06T01:27:47.806Z',\n",
       " 'Succeeded | Hamiltonian term 1 | ionq.qpu | 2021-05-06T01:20:39.1679175Z | 2021-05-06T01:27:56.779Z',\n",
       " 'Succeeded | Hamiltonian term 2 | ionq.qpu | 2021-05-06T01:20:47.9310132Z | 2021-05-06T01:28:05.876Z',\n",
       " 'Succeeded | Hamiltonian term 3 | ionq.qpu | 2021-05-06T01:20:58.5528459Z | 2021-05-06T01:28:15.744Z',\n",
       " 'Succeeded | Hamiltonian term 4 | ionq.qpu | 2021-05-06T01:21:07.3776298Z | 2021-05-06T01:28:25.005Z',\n",
       " 'Succeeded | Hamiltonian term 5 | ionq.qpu | 2021-05-06T01:21:16.1710463Z | 2021-05-06T01:28:34.117Z',\n",
       " 'Succeeded | Hamiltonian term 6 | ionq.qpu | 2021-05-06T01:21:27.272853Z | 2021-05-06T01:28:43.245Z',\n",
       " 'Succeeded | Hamiltonian term 7 | ionq.qpu | 2021-05-06T01:21:38.2052809Z | 2021-05-06T01:28:52.426Z',\n",
       " 'Succeeded | Hamiltonian term 8 | ionq.qpu | 2021-05-06T01:21:49.4659481Z | 2021-05-06T01:29:01.519Z',\n",
       " 'Succeeded | Hamiltonian term 9 | ionq.qpu | 2021-05-06T01:22:03.0275365Z | 2021-05-06T01:29:12.068Z',\n",
       " 'Succeeded | Hamiltonian term 12 | ionq.qpu | 2021-05-06T01:22:14.6967465Z | 2021-05-06T01:29:21.58Z',\n",
       " 'Succeeded | Hamiltonian term 13 | ionq.qpu | 2021-05-06T01:22:24.1297152Z | 2021-05-06T01:29:31.071Z',\n",
       " 'Succeeded | Hamiltonian term 16 | ionq.qpu | 2021-05-06T01:22:33.8162175Z | 2021-05-06T01:29:40.647Z',\n",
       " 'Succeeded | Hamiltonian term 17 | ionq.qpu | 2021-05-06T01:22:43.6351012Z | 2021-05-06T01:29:50.332Z']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jobs_status = [qsharp.azure.status(j) for j in jobs_hw]\n",
    "[\n",
    "    j.status + \" | \" + j.name + \" | \" + j.target + \" | \" + j.creation_time + \" | \" + j.end_execution_time \n",
    "    for j in jobs_status\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f263a26",
   "metadata": {},
   "source": [
    "Get results and estimate energy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "cd5fd7ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'0': 0.019, '1': 0.981},\n",
       " {'0': 0.016, '1': 0.984},\n",
       " {'0': 0.9770000000000001, '1': 0.023},\n",
       " {'0': 0.99, '1': 0.01},\n",
       " {'0': 0.9470000000000001, '1': 0.053},\n",
       " {'0': 0.057, '1': 0.9430000000000001},\n",
       " {'0': 0.030000000000000002, '1': 0.9700000000000001},\n",
       " {'0': 0.07300000000000001, '1': 0.927},\n",
       " {'0': 0.022, '1': 0.9780000000000001},\n",
       " {'0': 0.9780000000000001, '1': 0.022},\n",
       " {'0': 0.505, '1': 0.495},\n",
       " {'0': 0.511, '1': 0.48900000000000005},\n",
       " {'0': 0.491, '1': 0.509},\n",
       " {'0': 0.5, '1': 0.5}]"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = [qsharp.azure.output(j.id) for j in jobs_hw]\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "5c1e5479",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.9626770776219998"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calc_energy(coeffs, results)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
